//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.ruoyi.system.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.SysMapperUser;
import com.ruoyi.system.service.ISysMapperUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Api(
        tags = {"供电所系统用户Controller"}
)
@RestController
@RequestMapping({"/mapperUser"})
public class SysMapperUserController extends BaseController {
    @Autowired
    private ISysMapperUserService sysMapperUserService;

    public SysMapperUserController() {
    }

    @ApiOperation("查询供电所系统用户列表")
    @RequiresPermissions({"system:mapperUser:list"})
    @GetMapping({"/list"})
    public TableDataInfo list(SysMapperUser sysMapperUser) {
        this.startPage();
        List<SysMapperUser> list = this.sysMapperUserService.selectSysMapperUserList(sysMapperUser);
        return this.getDataTable(list);
    }

    @RequiresPermissions({"system:mapperUser:export"})
    @Log(
            title = "供电所系统用户",
            businessType = BusinessType.EXPORT
    )
    @PostMapping({"/export"})
    public void export(@RequestBody SysMapperUser sysMapperUser, HttpServletResponse response) throws IOException {
        List<SysMapperUser> list = this.sysMapperUserService.selectSysMapperUserList(sysMapperUser);
        ExcelUtil<SysMapperUser> util = new ExcelUtil(SysMapperUser.class);
        util.exportExcel(response, list, "供电所系统用户数据");
    }

    @RequiresPermissions({"system:mapperUser:query"})
    @GetMapping({"/info"})
    public AjaxResult getInfo(@RequestParam("mapperId") Long mapperId) {
        return AjaxResult.success(this.sysMapperUserService.selectSysMapperUserByMapperId(mapperId));
    }

    @RequiresPermissions({"system:mapperUser:add"})
    @Log(
            title = "供电所系统用户",
            businessType = BusinessType.INSERT
    )
    @PostMapping({"/insert"})
    public AjaxResult add(@RequestBody SysMapperUser sysMapperUser) {
        List<SysMapperUser> userList = this.sysMapperUserService.selectSysMapperUserByintegsysCodeAndErpCode(sysMapperUser);
        if (userList.size() > 0) {
            return AjaxResult.error(-1, "集成系统下已存在此账户");
        } else {
            this.sysMapperUserService.insertSysMapperUser(sysMapperUser);
            return this.success();
        }
    }

    @RequiresPermissions({"system:mapperUser:edit"})
    @Log(
            title = "供电所系统用户",
            businessType = BusinessType.UPDATE
    )
    @PostMapping({"/edit"})
    public AjaxResult edit(@RequestBody SysMapperUser sysMapperUser) {
        if (sysMapperUser.getMapperId() == null) {
            return AjaxResult.error(-1, "主键不能为空");
        } else {
            List<SysMapperUser> userList = this.sysMapperUserService.selectSysMapperUserByintegsysCodeAndErpCode(sysMapperUser);
            if (userList.size() == 1 && ((SysMapperUser)userList.get(0)).getMapperId() != sysMapperUser.getMapperId()) {
                return AjaxResult.error(-1, "集成系统下已存在此账户");
            } else {
                this.sysMapperUserService.updateSysMapperUser(sysMapperUser);
                return this.success();
            }
        }
    }

    @RequiresPermissions({"system:mapperUser:remove"})
    @Log(
            title = "供电所系统用户",
            businessType = BusinessType.DELETE
    )
    @PostMapping({"/remove"})
    public AjaxResult remove(@RequestBody Long[] mapperIds) {
        this.sysMapperUserService.deleteSysMapperUserByMapperIds(mapperIds);
        return this.success();
    }
}
